mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Krzysztof Kozlowski <krzk@kernel.org>,
	Thierry Reding <thierry.reding@kernel.org>,
	Jonathan Hunter <jonathanh@nvidia.com>,
	Joseph Lo <josephl@nvidia.com>,
	linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 2/8] memory: tegra210-emc: Stop timers on suspend and prevent training re-arm
Date: Wed,  2 Dec 2026 09:33:49 +0000	[thread overview]
Message-ID: <9fc4cf5403104b05b056574c2b2a60808f462312.1796203994.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796203994.git.lee@kernel.org>

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


  parent reply	other threads:[~2026-12-02  9:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:33 [PATCH 0/8] memory: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-02  9:33 ` [PATCH 1/8] memory: atmel-ebi: Use non-devm allocations for of_update_property() Lee Jones
2026-12-02  9:33 ` Lee Jones [this message]
2026-12-02  9:33 ` [PATCH 3/8] memory: stm32_omm: Depopulate child devices before disabling clocks Lee Jones
2026-12-02 13:54   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 4/8] memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK Lee Jones
2026-12-02 14:08   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation Lee Jones
2026-12-02 14:17   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 6/8] memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr() Lee Jones
2026-12-02 14:33   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 7/8] memory: emif: Avoid __fls(0) and underflow in of_get_ddr_info() Lee Jones
2026-12-02  9:33 ` [PATCH 8/8] memory: emif: Clear invalid custom_configs in get_device_details() Lee Jones

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=9fc4cf5403104b05b056574c2b2a60808f462312.1796203994.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=jonathanh@nvidia.com \
    --cc=josephl@nvidia.com \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=thierry.reding@kernel.org \
    /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®