public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Daniel Lezcano <daniel.lezcano@linaro.org>
To: rafael@kernel.org, daniel.lezcano@linaro.org
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Amit Kucheria" <amitk@kernel.org>,
	"Zhang Rui" <rui.zhang@intel.com>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Jonathan Hunter" <jonathanh@nvidia.com>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>,
	linux-tegra@vger.kernel.org (open list:TEGRA ARCHITECTURE
	SUPPORT)
Subject: [PATCH v3 16/20] thermal/drivers/tegra: Remove unneeded lock when setting a trip point
Date: Sun, 26 Feb 2023 23:54:02 +0100	[thread overview]
Message-ID: <20230226225406.979703-17-daniel.lezcano@linaro.org> (raw)
In-Reply-To: <20230226225406.979703-1-daniel.lezcano@linaro.org>

The function tegra_tsensor_enable_hw_channel() takes the thermal zone
lock to prevent "a potential" race with a call to set_trips()
callback.

The driver must not play with the thermal framework core code
internals.

The tegra_tsensor_enable_hw_channel() is called by:

 - the suspend / resume callbacks
 - the probe function after the thermal zones are registered

The thermal zone lock taken in this function is supposed to protect
from a call to the set_trips() callback which writes in the same
register.

The potential race is when suspend / resume are called at the same
time as set_trips. This one is called only in
thermal_zone_device_update().

 - At suspend time, the 'in_suspend' is set, thus the
   thermal_zone_device_update() bails out immediately and set_trips is
   not called during this moment.

 - At resume time, the thermal zone is updated at PM_POST_SUSPEND,
   thus the driver has already set the TH2 temperature.

 - At probe time, we register the thermal zone and then we set the
   TH2. The only scenario I can see so far is the interrupt fires, the
   thermal_zone_update() is called exactly at the moment
   tegra_tsensor_enable_hw_channel() a few lines after registering it.

Disable the interrupt before setting up the hw channels and then
enable it. We close the potential race window without using the
thermal zone's lock.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 drivers/thermal/tegra/tegra30-tsensor.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/thermal/tegra/tegra30-tsensor.c b/drivers/thermal/tegra/tegra30-tsensor.c
index 4b2ea17910cd..3506c3f3c474 100644
--- a/drivers/thermal/tegra/tegra30-tsensor.c
+++ b/drivers/thermal/tegra/tegra30-tsensor.c
@@ -359,9 +359,6 @@ static int tegra_tsensor_enable_hw_channel(const struct tegra_tsensor *ts,
 
 	tegra_tsensor_get_hw_channel_trips(tzd, &hot_trip, &crit_trip);
 
-	/* prevent potential racing with tegra_tsensor_set_trips() */
-	mutex_lock(&tzd->lock);
-
 	dev_info_once(ts->dev, "ch%u: PMC emergency shutdown trip set to %dC\n",
 		      id, DIV_ROUND_CLOSEST(crit_trip, 1000));
 
@@ -404,8 +401,6 @@ static int tegra_tsensor_enable_hw_channel(const struct tegra_tsensor *ts,
 	val |= FIELD_PREP(TSENSOR_SENSOR0_CONFIG0_INTR_THERMAL_RST_EN, 1);
 	writel_relaxed(val, tsc->regs + TSENSOR_SENSOR0_CONFIG0);
 
-	mutex_unlock(&tzd->lock);
-
 	err = thermal_zone_device_enable(tzd);
 	if (err) {
 		dev_err(ts->dev, "ch%u: failed to enable zone: %d\n", id, err);
@@ -592,12 +587,24 @@ static int tegra_tsensor_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, err,
 				     "failed to request interrupt\n");
 
+	/*
+	 * Disable the interrupt so set_trips() can not be called
+	 * while we are setting up the register
+	 * TSENSOR_SENSOR0_CONFIG1. With this we close a potential
+	 * race window where we are setting up the TH2 and the
+	 * temperature hits TH1 resulting to an update of the
+	 * TSENSOR_SENSOR0_CONFIG1 register in the ISR.
+	 */
+	disable_irq(irq);
+
 	for (i = 0; i < ARRAY_SIZE(ts->ch); i++) {
 		err = tegra_tsensor_enable_hw_channel(ts, i);
 		if (err)
 			return err;
 	}
 
+	enable_irq(irq);
+
 	return 0;
 }
 
-- 
2.34.1


  parent reply	other threads:[~2023-02-26 22:56 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-26 22:53 [PATCH v3 00/20] Self-encapsulate the thermal zone device structure Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 01/20] thermal/core: Add a thermal zone 'devdata' accessor Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 02/20] thermal/core: Use the thermal zone 'devdata' accessor in thermal located drivers Daniel Lezcano
2023-02-27  1:57   ` kernel test robot
2023-02-27  6:43   ` kernel test robot
2023-02-26 22:53 ` [PATCH v3 03/20] thermal/core: Use the thermal zone 'devdata' accessor in hwmon " Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 04/20] thermal/core: Use the thermal zone 'devdata' accessor in remaining drivers Daniel Lezcano
2023-02-26 22:59   ` Damien Le Moal
2023-02-26 22:53 ` [PATCH v3 05/20] thermal/core: Show a debug message when get_temp() fails Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 06/20] thermal: Remove debug or error messages in get_temp() ops Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 07/20] thermal/hwmon: Do not set no_hwmon before calling thermal_add_hwmon_sysfs() Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 08/20] thermal/hwmon: Use the right device for devm_thermal_add_hwmon_sysfs() Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 09/20] thermal: Don't use 'device' internal thermal zone structure field Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 10/20] thermal/core: Add thermal_zone_device structure 'type' accessor Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 11/20] thermal/drivers/spear: Don't use tz->device but pdev->dev Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 12/20] thermal: Add a thermal zone id accessor Daniel Lezcano
2023-02-26 22:53 ` [PATCH v3 13/20] thermal: Use thermal_zone_device_type() accessor Daniel Lezcano
2023-02-26 22:54 ` [PATCH v3 14/20] thermal/drivers/da9062: Don't access the thermal zone device fields Daniel Lezcano
2023-02-27 18:14   ` Daniel Lezcano
2023-02-28 10:04     ` DLG Adam Ward
2023-02-28 11:01       ` Daniel Lezcano
2023-02-26 22:54 ` [PATCH v3 15/20] thermal/hwmon: Use the thermal_core.h header Daniel Lezcano
2023-02-27 14:21   ` Jean Delvare
2023-02-26 22:54 ` Daniel Lezcano [this message]
2023-02-26 22:54 ` [PATCH v3 17/20] thermal/tegra: Do not enable the thermal zone, it is already enabled Daniel Lezcano
2023-02-26 22:54 ` [PATCH v3 18/20] thermal/drivers/acerhdf: Make interval setting only at module load time Daniel Lezcano
2023-02-26 22:54 ` [PATCH v3 19/20] thermal/drivers/acerhdf: Remove pointless governor test Daniel Lezcano
2023-02-26 22:54 ` [PATCH v3 20/20] thermal/traces: Replace the thermal zone structure parameter with the field value Daniel Lezcano
2023-02-27 15:07   ` Steven Rostedt
2023-02-27 16:01     ` Daniel Lezcano

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=20230226225406.979703-17-daniel.lezcano@linaro.org \
    --to=daniel.lezcano@linaro.org \
    --cc=amitk@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=f.fainelli@gmail.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=jonathanh@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=thierry.reding@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