From: Mark Tseng <chun-jen.tseng@mediatek.com>
To: "Rafael J . Wysocki" <rafael@kernel.org>,
Viresh Kumar <viresh.kumar@linaro.org>,
MyungJoo Ham <myungjoo.ham@samsung.com>,
Kyungmin Park <kyungmin.park@samsung.com>,
Chanwoo Choi <cw00.choi@samsung.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>
Cc: <linux-pm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-mediatek@lists.infradead.org>,
<Project_Global_Chrome_Upstream_Group@mediatek.com>,
<chun-jen.tseng@mediatek.com>
Subject: [PATCH v3 1/3] cpufreq: mediatek: using global lock avoid race condition
Date: Fri, 14 Feb 2025 15:43:32 +0800 [thread overview]
Message-ID: <20250214074353.1169864-2-chun-jen.tseng@mediatek.com> (raw)
In-Reply-To: <20250214074353.1169864-1-chun-jen.tseng@mediatek.com>
In mtk_cpufreq_set_target() is re-enter function but the mutex lock
decalre in mtk_cpu_dvfs_info structure for each policy. It should
change to global variable for critical session avoid race condition
with 2 or more policy.
add mtk_cpufreq_get() replace cpufreq_generic_get() and use global lock
avoid return wrong clock.
Signed-off-by: Mark Tseng <chun-jen.tseng@mediatek.com>
---
drivers/cpufreq/mediatek-cpufreq.c | 39 ++++++++++++++++++++++--------
1 file changed, 29 insertions(+), 10 deletions(-)
diff --git a/drivers/cpufreq/mediatek-cpufreq.c b/drivers/cpufreq/mediatek-cpufreq.c
index 2656b88db378..07d5958e106a 100644
--- a/drivers/cpufreq/mediatek-cpufreq.c
+++ b/drivers/cpufreq/mediatek-cpufreq.c
@@ -49,8 +49,6 @@ struct mtk_cpu_dvfs_info {
bool need_voltage_tracking;
int vproc_on_boot;
int pre_vproc;
- /* Avoid race condition for regulators between notify and policy */
- struct mutex reg_lock;
struct notifier_block opp_nb;
unsigned int opp_cpu;
unsigned long current_freq;
@@ -59,6 +57,9 @@ struct mtk_cpu_dvfs_info {
bool ccifreq_bound;
};
+/* Avoid race condition for regulators between notify and policy */
+static DEFINE_MUTEX(mtk_policy_lock);
+
static struct platform_device *cpufreq_pdev;
static LIST_HEAD(dvfs_info_list);
@@ -209,12 +210,12 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
long freq_hz, pre_freq_hz;
int vproc, pre_vproc, inter_vproc, target_vproc, ret;
+ mutex_lock(&mtk_policy_lock);
+
inter_vproc = info->intermediate_voltage;
pre_freq_hz = clk_get_rate(cpu_clk);
- mutex_lock(&info->reg_lock);
-
if (unlikely(info->pre_vproc <= 0))
pre_vproc = regulator_get_voltage(info->proc_reg);
else
@@ -308,7 +309,7 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
info->current_freq = freq_hz;
out:
- mutex_unlock(&info->reg_lock);
+ mutex_unlock(&mtk_policy_lock);
return ret;
}
@@ -316,19 +317,20 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
unsigned long event, void *data)
{
- struct dev_pm_opp *opp = data;
+ struct dev_pm_opp *opp;
struct dev_pm_opp *new_opp;
struct mtk_cpu_dvfs_info *info;
unsigned long freq, volt;
struct cpufreq_policy *policy;
int ret = 0;
+ mutex_lock(&mtk_policy_lock);
+ opp = data;
info = container_of(nb, struct mtk_cpu_dvfs_info, opp_nb);
if (event == OPP_EVENT_ADJUST_VOLTAGE) {
freq = dev_pm_opp_get_freq(opp);
- mutex_lock(&info->reg_lock);
if (info->current_freq == freq) {
volt = dev_pm_opp_get_voltage(opp);
ret = mtk_cpufreq_set_voltage(info, volt);
@@ -336,7 +338,6 @@ static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
dev_err(info->cpu_dev,
"failed to scale voltage: %d\n", ret);
}
- mutex_unlock(&info->reg_lock);
} else if (event == OPP_EVENT_DISABLE) {
freq = dev_pm_opp_get_freq(opp);
@@ -361,6 +362,7 @@ static int mtk_cpufreq_opp_notifier(struct notifier_block *nb,
}
}
}
+ mutex_unlock(&mtk_policy_lock);
return notifier_from_errno(ret);
}
@@ -495,7 +497,6 @@ static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
dev_pm_opp_put(opp);
- mutex_init(&info->reg_lock);
info->current_freq = clk_get_rate(info->cpu_clk);
info->opp_cpu = cpu;
@@ -607,13 +608,31 @@ static void mtk_cpufreq_exit(struct cpufreq_policy *policy)
dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
}
+static unsigned int mtk_cpufreq_get(unsigned int cpu)
+{
+ struct mtk_cpu_dvfs_info *info;
+ unsigned long current_freq;
+
+ mutex_lock(&mtk_policy_lock);
+ info = mtk_cpu_dvfs_info_lookup(cpu);
+ if (!info) {
+ mutex_unlock(&mtk_policy_lock);
+ return 0;
+ }
+
+ current_freq = info->current_freq / 1000;
+ mutex_unlock(&mtk_policy_lock);
+
+ return current_freq;
+}
+
static struct cpufreq_driver mtk_cpufreq_driver = {
.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
CPUFREQ_IS_COOLING_DEV,
.verify = cpufreq_generic_frequency_table_verify,
.target_index = mtk_cpufreq_set_target,
- .get = cpufreq_generic_get,
+ .get = mtk_cpufreq_get,
.init = mtk_cpufreq_init,
.exit = mtk_cpufreq_exit,
.register_em = cpufreq_register_em_with_opp,
--
2.45.2
next prev parent reply other threads:[~2025-02-14 7:44 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-14 7:43 [PATCH v3 0/3] fixed mediatek-cpufreq has multi policy concurrency issue Mark Tseng
2025-02-14 7:43 ` Mark Tseng [this message]
2025-02-19 5:42 ` [PATCH v3 1/3] cpufreq: mediatek: using global lock avoid race condition Viresh Kumar
2025-03-20 8:22 ` Chun-Jen Tseng (曾俊仁)
2025-03-21 4:56 ` Viresh Kumar
2025-03-21 5:32 ` Chun-Jen Tseng (曾俊仁)
2025-03-21 6:01 ` Viresh Kumar
2025-03-24 3:21 ` Chun-Jen Tseng (曾俊仁)
2025-03-24 5:43 ` Viresh Kumar
2025-04-14 8:42 ` Chun-Jen Tseng (曾俊仁)
2025-04-16 8:05 ` Viresh Kumar
2025-08-28 13:26 ` Chen-Yu Tsai
2025-08-29 5:47 ` Viresh Kumar
2025-02-19 7:23 ` Dan Carpenter
2025-03-20 8:25 ` Chun-Jen Tseng (曾俊仁)
2025-02-14 7:43 ` [PATCH v3 2/3] cpufreq: mediatek: Add CPUFREQ_ASYNC_NOTIFICATION flag Mark Tseng
2025-02-19 5:45 ` Viresh Kumar
2025-03-20 8:34 ` Chun-Jen Tseng (曾俊仁)
2025-03-21 4:59 ` Viresh Kumar
2025-02-14 7:43 ` [PATCH v3 3/3] cpufreq: mediatek: data safety protect Mark Tseng
2025-02-19 5:49 ` Viresh Kumar
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=20250214074353.1169864-2-chun-jen.tseng@mediatek.com \
--to=chun-jen.tseng@mediatek.com \
--cc=Project_Global_Chrome_Upstream_Group@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=cw00.choi@samsung.com \
--cc=kyungmin.park@samsung.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-pm@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
--cc=myungjoo.ham@samsung.com \
--cc=rafael@kernel.org \
--cc=viresh.kumar@linaro.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®