Skip to content

Commit 6889125

Browse files
committed
cpufreq/powernow-k8: workqueue user shouldn't migrate the kworker to another CPU
powernowk8_target() runs off a per-cpu work item and if the cpufreq_policy->cpu is different from the current one, it migrates the kworker to the target CPU by manipulating current->cpus_allowed. The function migrates the kworker back to the original CPU but this is still broken. Workqueue concurrency management requires the kworkers to stay on the same CPU and powernowk8_target() ends up triggerring BUG_ON(rq != this_rq()) in try_to_wake_up_local() if it contends on fidvid_mutex and sleeps. It is unclear why this bug is being reported now. Duncan says it appeared to be a regression of 3.6-rc1 and couldn't reproduce it on 3.5. Bisection seemed to point to 63d95a9 "workqueue: use @pool instead of @gcwq or @cpu where applicable" which is an non-functional change. Given that the reproduce case sometimes took upto days to trigger, it's easy to be misled while bisecting. Maybe something made contention on fidvid_mutex more likely? I don't know. This patch fixes the bug by using work_on_cpu() instead if @pol->cpu isn't the same as the current one. The code assumes that cpufreq_policy->cpu is kept online by the caller, which Rafael tells me is the case. stable: ed48ece ("workqueue: reimplement work_on_cpu() using system_wq") should be applied before this; otherwise, the behavior could be horrible. Signed-off-by: Tejun Heo <[email protected]> Reported-by: Duncan <[email protected]> Tested-by: Duncan <[email protected]> Cc: Rafael J. Wysocki <[email protected]> Cc: Andreas Herrmann <[email protected]> Cc: [email protected] Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=47301
1 parent ed48ece commit 6889125

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

drivers/cpufreq/powernow-k8.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/slab.h>
3636
#include <linux/string.h>
3737
#include <linux/cpumask.h>
38-
#include <linux/sched.h> /* for current / set_cpus_allowed() */
3938
#include <linux/io.h>
4039
#include <linux/delay.h>
4140

@@ -1139,46 +1138,40 @@ static int transition_frequency_pstate(struct powernow_k8_data *data,
11391138
return res;
11401139
}
11411140

1142-
/* Driver entry point to switch to the target frequency */
1143-
static int powernowk8_target(struct cpufreq_policy *pol,
1144-
unsigned targfreq, unsigned relation)
1141+
struct powernowk8_target_arg {
1142+
struct cpufreq_policy *pol;
1143+
unsigned targfreq;
1144+
unsigned relation;
1145+
};
1146+
1147+
static long powernowk8_target_fn(void *arg)
11451148
{
1146-
cpumask_var_t oldmask;
1149+
struct powernowk8_target_arg *pta = arg;
1150+
struct cpufreq_policy *pol = pta->pol;
1151+
unsigned targfreq = pta->targfreq;
1152+
unsigned relation = pta->relation;
11471153
struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
11481154
u32 checkfid;
11491155
u32 checkvid;
11501156
unsigned int newstate;
1151-
int ret = -EIO;
1157+
int ret;
11521158

11531159
if (!data)
11541160
return -EINVAL;
11551161

11561162
checkfid = data->currfid;
11571163
checkvid = data->currvid;
11581164

1159-
/* only run on specific CPU from here on. */
1160-
/* This is poor form: use a workqueue or smp_call_function_single */
1161-
if (!alloc_cpumask_var(&oldmask, GFP_KERNEL))
1162-
return -ENOMEM;
1163-
1164-
cpumask_copy(oldmask, tsk_cpus_allowed(current));
1165-
set_cpus_allowed_ptr(current, cpumask_of(pol->cpu));
1166-
1167-
if (smp_processor_id() != pol->cpu) {
1168-
printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1169-
goto err_out;
1170-
}
1171-
11721165
if (pending_bit_stuck()) {
11731166
printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1174-
goto err_out;
1167+
return -EIO;
11751168
}
11761169

11771170
pr_debug("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
11781171
pol->cpu, targfreq, pol->min, pol->max, relation);
11791172

11801173
if (query_current_values_with_pending_wait(data))
1181-
goto err_out;
1174+
return -EIO;
11821175

11831176
if (cpu_family != CPU_HW_PSTATE) {
11841177
pr_debug("targ: curr fid 0x%x, vid 0x%x\n",
@@ -1196,7 +1189,7 @@ static int powernowk8_target(struct cpufreq_policy *pol,
11961189

11971190
if (cpufreq_frequency_table_target(pol, data->powernow_table,
11981191
targfreq, relation, &newstate))
1199-
goto err_out;
1192+
return -EIO;
12001193

12011194
mutex_lock(&fidvid_mutex);
12021195

@@ -1209,9 +1202,8 @@ static int powernowk8_target(struct cpufreq_policy *pol,
12091202
ret = transition_frequency_fidvid(data, newstate);
12101203
if (ret) {
12111204
printk(KERN_ERR PFX "transition frequency failed\n");
1212-
ret = 1;
12131205
mutex_unlock(&fidvid_mutex);
1214-
goto err_out;
1206+
return 1;
12151207
}
12161208
mutex_unlock(&fidvid_mutex);
12171209

@@ -1220,12 +1212,25 @@ static int powernowk8_target(struct cpufreq_policy *pol,
12201212
data->powernow_table[newstate].index);
12211213
else
12221214
pol->cur = find_khz_freq_from_fid(data->currfid);
1223-
ret = 0;
12241215

1225-
err_out:
1226-
set_cpus_allowed_ptr(current, oldmask);
1227-
free_cpumask_var(oldmask);
1228-
return ret;
1216+
return 0;
1217+
}
1218+
1219+
/* Driver entry point to switch to the target frequency */
1220+
static int powernowk8_target(struct cpufreq_policy *pol,
1221+
unsigned targfreq, unsigned relation)
1222+
{
1223+
struct powernowk8_target_arg pta = { .pol = pol, .targfreq = targfreq,
1224+
.relation = relation };
1225+
1226+
/*
1227+
* Must run on @pol->cpu. cpufreq core is responsible for ensuring
1228+
* that we're bound to the current CPU and pol->cpu stays online.
1229+
*/
1230+
if (smp_processor_id() == pol->cpu)
1231+
return powernowk8_target_fn(&pta);
1232+
else
1233+
return work_on_cpu(pol->cpu, powernowk8_target_fn, &pta);
12291234
}
12301235

12311236
/* Driver entry point to verify the policy and range of frequencies */

0 commit comments

Comments
 (0)