* [PATCH 2/8] memory: tegra210-emc: Stop timers on suspend and prevent training re-arm
2026-12-02 9:33 [PATCH 0/8] memory: Fix Critical and High issues reported by Sashiko Lee Jones
@ 2026-12-02 9:33 ` Lee Jones
0 siblings, 0 replies; 2+ messages in thread
From: Lee Jones @ 2026-12-02 9:33 UTC (permalink / raw)
To: lee, Krzysztof Kozlowski, Thierry Reding, Jonathan Hunter,
Joseph Lo, linux-kernel, linux-tegra
Cc: stable
tegra210_emc_train() unconditionally calls mod_timer(&emc->training, ...)
after releasing emc->lock. When tegra210_emc_training_stop() deletes
the timer under emc->lock while tegra210_emc_train() is already running
on another CPU, the callback can race with the stop, execute
periodic_compensation() on an untrained rate, and re-arm itself.
In addition, tegra210_emc_suspend() detaches the EMC clock without
synchronously stopping the refresh polling timer or the periodic
training timer, allowing timer callbacks to touch EMC hardware or
re-enable training after suspend.
Track whether periodic training is enabled under emc->lock so
tegra210_emc_train() only runs and re-arms while training is active.
Synchronously stop refresh polling and periodic training in
tegra210_emc_suspend() before detaching the EMC clock, and restart
periodic training (if required by the resumed timing and not already
restarted by clk_set_rate()) and refresh polling in
tegra210_emc_resume().
Fixes: 10de21148f7d ("memory: tegra: Add EMC scaling support code for Tegra210")
Closes: https://sashiko.dev/#/bug/linux-47538f77-fb50-4a06-b49b-92dbee8b538b
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/memory/tegra/tegra210-emc-core.c | 37 +++++++++++++++++++-----
drivers/memory/tegra/tegra210-emc.h | 2 ++
2 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/drivers/memory/tegra/tegra210-emc-core.c b/drivers/memory/tegra/tegra210-emc-core.c
index 044ec6f1bc32..06aba71d8fb6 100644
--- a/drivers/memory/tegra/tegra210-emc-core.c
+++ b/drivers/memory/tegra/tegra210-emc-core.c
@@ -560,28 +560,29 @@ static void tegra210_emc_train(struct timer_list *timer)
struct tegra210_emc *emc = timer_container_of(emc, timer, training);
unsigned long flags;
- if (!emc->last)
- return;
-
spin_lock_irqsave(&emc->lock, flags);
- if (emc->sequence->periodic_compensation)
- emc->sequence->periodic_compensation(emc);
+ if (emc->training_enabled && emc->last) {
+ if (emc->sequence->periodic_compensation)
+ emc->sequence->periodic_compensation(emc);
+
+ mod_timer(&emc->training,
+ jiffies + msecs_to_jiffies(emc->training_interval));
+ }
spin_unlock_irqrestore(&emc->lock, flags);
-
- mod_timer(&emc->training,
- jiffies + msecs_to_jiffies(emc->training_interval));
}
static void tegra210_emc_training_start(struct tegra210_emc *emc)
{
+ emc->training_enabled = true;
mod_timer(&emc->training,
jiffies + msecs_to_jiffies(emc->training_interval));
}
static void tegra210_emc_training_stop(struct tegra210_emc *emc)
{
+ emc->training_enabled = false;
timer_delete(&emc->training);
}
@@ -2001,6 +2002,7 @@ static void tegra210_emc_remove(struct platform_device *pdev)
static int __maybe_unused tegra210_emc_suspend(struct device *dev)
{
struct tegra210_emc *emc = dev_get_drvdata(dev);
+ unsigned long flags;
int err;
err = clk_rate_exclusive_get(emc->clk);
@@ -2009,9 +2011,19 @@ static int __maybe_unused tegra210_emc_suspend(struct device *dev)
return err;
}
+ emc->resume_refresh_poll = atomic_read(&emc->refresh_poll) > 0;
+ if (emc->resume_refresh_poll)
+ tegra210_emc_poll_refresh_stop(emc);
+
emc->resume_rate = clk_get_rate(emc->clk);
clk_set_rate(emc->clk, 204000000);
+
+ spin_lock_irqsave(&emc->lock, flags);
+ tegra210_emc_training_stop(emc);
+ spin_unlock_irqrestore(&emc->lock, flags);
+ timer_delete_sync(&emc->training);
+
tegra210_clk_emc_detach(emc->clk);
dev_dbg(dev, "suspending at %lu Hz\n", clk_get_rate(emc->clk));
@@ -2022,6 +2034,7 @@ static int __maybe_unused tegra210_emc_suspend(struct device *dev)
static int __maybe_unused tegra210_emc_resume(struct device *dev)
{
struct tegra210_emc *emc = dev_get_drvdata(dev);
+ unsigned long flags;
int err;
err = tegra210_clk_emc_attach(emc->clk, &emc->provider);
@@ -2033,6 +2046,14 @@ static int __maybe_unused tegra210_emc_resume(struct device *dev)
clk_set_rate(emc->clk, emc->resume_rate);
clk_rate_exclusive_put(emc->clk);
+ spin_lock_irqsave(&emc->lock, flags);
+ if (emc->last && emc->last->periodic_training && !emc->training_enabled)
+ tegra210_emc_training_start(emc);
+ spin_unlock_irqrestore(&emc->lock, flags);
+
+ if (emc->resume_refresh_poll)
+ tegra210_emc_poll_refresh_start(emc);
+
dev_dbg(dev, "resuming at %lu Hz\n", clk_get_rate(emc->clk));
return 0;
diff --git a/drivers/memory/tegra/tegra210-emc.h b/drivers/memory/tegra/tegra210-emc.h
index 8988bcf15290..44379034da8b 100644
--- a/drivers/memory/tegra/tegra210-emc.h
+++ b/drivers/memory/tegra/tegra210-emc.h
@@ -912,6 +912,7 @@ struct tegra210_emc {
unsigned int training_interval;
struct timer_list training;
+ bool training_enabled;
enum tegra210_emc_refresh refresh;
unsigned int refresh_poll_interval;
@@ -923,6 +924,7 @@ struct tegra210_emc {
int clkchange_delay;
unsigned long resume_rate;
+ bool resume_refresh_poll;
struct {
struct dentry *root;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread