public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rusty Russell <rusty@rustcorp.com.au>
To: Yinghai Lu <yinghai@kernel.org>
Cc: Avi Kivity <avi@redhat.com>, Ingo Molnar <mingo@elte.hu>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Dave Jones <davej@redhat.com>,
	cpufreq@vger.kernel.org
Subject: Re: [PATCH 4/6] x86/cpufreq: use cpumask_copy instead of =
Date: Tue, 9 Jun 2009 16:27:42 +0930	[thread overview]
Message-ID: <200906091627.45411.rusty@rustcorp.com.au> (raw)
In-Reply-To: <4A2AE570.2010307@kernel.org>

On Sun, 7 Jun 2009 07:23:52 am Yinghai Lu wrote:
> so later could use nr_cpumask_bits in cpumask_size when MAXSMP is used

I have a (more ambitious) patch for this in my queue, which weans it off this
entirely:

Subject: cpumask: avoid playing with cpus_allowed in powernow-k8.c
From: Rusty Russell <rusty@rustcorp.com.au>

It's generally a very bad idea to mug some process's cpumask: it could
legitimately and reasonably be changed by root, which could break us
(if done before our code) or them (if we restore the wrong value).

I use work_on_cpu, which is slightly less efficient than the old code,
but the code is complex enough that using smp_call_function_single()
is not trivial.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
To: davej@redhat.com
To: cpufreq@vger.kernel.org
Cc: mark.langsdorf@amd.com
---
 arch/x86/kernel/cpu/cpufreq/powernow-k8.c |  128 +++++++++++++++---------------
 1 file changed, 64 insertions(+), 64 deletions(-)

diff --git a/arch/x86/kernel/cpu/cpufreq/powernow-k8.c b/arch/x86/kernel/cpu/cpufreq/powernow-k8.c
--- a/arch/x86/kernel/cpu/cpufreq/powernow-k8.c
+++ b/arch/x86/kernel/cpu/cpufreq/powernow-k8.c
@@ -510,19 +510,10 @@ static int core_voltage_post_transition(
 	return 0;
 }
 
-static int check_supported_cpu(unsigned int cpu)
+static long check_supported_cpu(void *unused)
 {
-	cpumask_t oldmask;
 	u32 eax, ebx, ecx, edx;
-	unsigned int rc = 0;
-
-	oldmask = current->cpus_allowed;
-	set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-
-	if (smp_processor_id() != cpu) {
-		printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
-		goto out;
-	}
+	unsigned int rc = -ENODEV;
 
 	if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
 		goto out;
@@ -562,10 +553,9 @@ static int check_supported_cpu(unsigned 
 			goto out;
 	}
 
-	rc = 1;
+	rc = 0;
 
 out:
-	set_cpus_allowed_ptr(current, &oldmask);
 	return rc;
 }
 
@@ -1140,11 +1130,16 @@ static int transition_frequency_pstate(s
 	return res;
 }
 
-/* Driver entry point to switch to the target frequency */
-static int powernowk8_target(struct cpufreq_policy *pol,
-		unsigned targfreq, unsigned relation)
+struct target_data {
+	struct cpufreq_policy *pol;
+	unsigned targfreq;
+	unsigned relation;
+};
+
+static long powernowk8_target_on_cpu(void *_tdata)
 {
-	cpumask_t oldmask;
+	struct target_data *tdata = _tdata;
+	struct cpufreq_policy *pol = tdata->pol;
 	struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
 	u32 checkfid;
 	u32 checkvid;
@@ -1157,22 +1152,13 @@ static int powernowk8_target(struct cpuf
 	checkfid = data->currfid;
 	checkvid = data->currvid;
 
-	/* only run on specific CPU from here on */
-	oldmask = current->cpus_allowed;
-	set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
-
-	if (smp_processor_id() != pol->cpu) {
-		printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
-		goto err_out;
-	}
-
 	if (pending_bit_stuck()) {
 		printk(KERN_ERR PFX "failing targ, change pending bit set\n");
 		goto err_out;
 	}
 
 	dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
-		pol->cpu, targfreq, pol->min, pol->max, relation);
+		pol->cpu, tdata->targfreq, pol->min, pol->max, tdata->relation);
 
 	if (query_current_values_with_pending_wait(data))
 		goto err_out;
@@ -1192,7 +1178,8 @@ static int powernowk8_target(struct cpuf
 	}
 
 	if (cpufreq_frequency_table_target(pol, data->powernow_table,
-				targfreq, relation, &newstate))
+					   tdata->targfreq, tdata->relation,
+					   &newstate))
 		goto err_out;
 
 	mutex_lock(&fidvid_mutex);
@@ -1219,10 +1206,19 @@ static int powernowk8_target(struct cpuf
 	ret = 0;
 
 err_out:
-	set_cpus_allowed_ptr(current, &oldmask);
 	return ret;
 }
 
+/* Driver entry point to switch to the target frequency */
+static int powernowk8_target(struct cpufreq_policy *pol,
+			     unsigned targfreq, unsigned relation)
+{
+	struct target_data tdata = { .pol = pol,
+				     .targfreq = targfreq,
+				     .relation = relation };
+	return work_on_cpu(pol->cpu, powernowk8_target_on_cpu, &tdata);
+}
+
 /* Driver entry point to verify the policy and range of frequencies */
 static int powernowk8_verify(struct cpufreq_policy *pol)
 {
@@ -1234,6 +1230,29 @@ static int powernowk8_verify(struct cpuf
 	return cpufreq_frequency_table_verify(pol, data->powernow_table);
 }
 
+static long __cpuinit powernowk8_cpu_init_on_cpu(void *_data)
+{
+	struct powernow_k8_data *data = _data;
+
+	if (smp_processor_id() != data->cpu) {
+		printk(KERN_ERR PFX "limiting to cpu %u failed\n", data->cpu);
+		return -EIO;
+	}
+
+	if (pending_bit_stuck()) {
+		printk(KERN_ERR PFX "failing init, change pending bit set\n");
+		return -ENODEV;
+	}
+
+	if (query_current_values_with_pending_wait(data))
+		return -ENODEV;
+
+	if (cpu_family == CPU_OPTERON)
+		fidvid_msr_init();
+
+	return 0;
+}
+
 static const char ACPI_PSS_BIOS_BUG_MSG[] =
 	KERN_ERR FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
 	KERN_ERR FW_BUG PFX "Try again with latest BIOS.\n";
@@ -1288,27 +1307,9 @@ static int __cpuinit powernowk8_cpu_init
 		pol->cpuinfo.transition_latency = get_transition_latency(data);
 
 	/* only run on specific CPU from here on */
-	oldmask = current->cpus_allowed;
-	set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
-
-	if (smp_processor_id() != pol->cpu) {
-		printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
-		goto err_out_unmask;
-	}
-
-	if (pending_bit_stuck()) {
-		printk(KERN_ERR PFX "failing init, change pending bit set\n");
-		goto err_out_unmask;
-	}
-
-	if (query_current_values_with_pending_wait(data))
-		goto err_out_unmask;
-
-	if (cpu_family == CPU_OPTERON)
-		fidvid_msr_init();
-
-	/* run on any CPU again */
-	set_cpus_allowed_ptr(current, &oldmask);
+	rc = work_on_cpu(data->cpu, powernowk8_cpu_init_on_cpu, data);
+	if (rc != 0)
+		goto err_out_exit_acpi;
 
 	if (cpu_family == CPU_HW_PSTATE)
 		cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
@@ -1345,8 +1346,7 @@ static int __cpuinit powernowk8_cpu_init
 
 	return 0;
 
-err_out_unmask:
-	set_cpus_allowed_ptr(current, &oldmask);
+err_out_exit_acpi:
 	powernow_k8_cpu_exit_acpi(data);
 
 err_out:
@@ -1371,12 +1371,20 @@ static int __devexit powernowk8_cpu_exit
 	return 0;
 }
 
+static void query_values_on_cpu(void *_err)
+{
+	int *err = _err;
+	struct powernow_k8_data *data = __get_cpu_var(powernow_data);
+
+	*err = query_current_values_with_pending_wait(data);
+}
+
 static unsigned int powernowk8_get(unsigned int cpu)
 {
 	struct powernow_k8_data *data;
-	cpumask_t oldmask = current->cpus_allowed;
 	unsigned int khz = 0;
 	unsigned int first;
+	int err;
 
 	first = cpumask_first(cpu_core_mask(cpu));
 	data = per_cpu(powernow_data, first);
@@ -1384,15 +1392,8 @@ static unsigned int powernowk8_get(unsig
 	if (!data)
 		return -EINVAL;
 
-	set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-	if (smp_processor_id() != cpu) {
-		printk(KERN_ERR PFX
-			"limiting to CPU %d failed in powernowk8_get\n", cpu);
-		set_cpus_allowed_ptr(current, &oldmask);
-		return 0;
-	}
-
-	if (query_current_values_with_pending_wait(data))
+	smp_call_function_single(first, query_values_on_cpu, &err, true);
+	if (err)
 		goto out;
 
 	if (cpu_family == CPU_HW_PSTATE)
@@ -1403,7 +1404,6 @@ static unsigned int powernowk8_get(unsig
 
 
 out:
-	set_cpus_allowed_ptr(current, &oldmask);
 	return khz;
 }
 
@@ -1429,7 +1429,7 @@ static int __cpuinit powernowk8_init(voi
 	unsigned int i, supported_cpus = 0;
 
 	for_each_online_cpu(i) {
-		if (check_supported_cpu(i))
+		if (work_on_cpu(i, check_supported_cpu, NULL) == 0)
 			supported_cpus++;
 	}
 


  reply	other threads:[~2009-06-09  6:57 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-04 21:00 [PATCH] kvm: fix kvm reboot crash when MAXSMP is used Yinghai Lu
2009-06-04 21:01 ` [PATCH] cpumask: alloc blank cpumask left over Yinghai Lu
2009-06-05  4:58   ` Rusty Russell
2009-06-05  5:18     ` Avi Kivity
2009-06-05  5:56     ` Yinghai Lu
2009-06-05 13:41       ` Rusty Russell
2009-06-05 17:34         ` Linus Torvalds
2009-06-05 17:46           ` Yinghai Lu
2009-06-05 17:57           ` Yinghai Lu
2009-06-06 23:40             ` Rusty Russell
2009-06-06 23:43           ` Rusty Russell
2009-06-06  9:22         ` Avi Kivity
2009-06-06  9:36           ` Yinghai Lu
2009-06-06  9:39             ` Avi Kivity
2009-06-06 10:57               ` Yinghai Lu
2009-06-06 21:50                 ` [PATCH 1/6] cpumask: introduce zalloc_cpumask_var Yinghai Lu
2009-06-06 21:51                   ` Subject: [PATCH 2/6] cpumask: alloc zeroed cpumask for static cpumask_var_ts Yinghai Lu
2009-06-06 21:52                   ` [PATCH 3/6] kvm: fix kvm reboot crash when MAXSMP is used Yinghai Lu
2009-06-06 21:53                   ` [PATCH 4/6] x86/cpufreq: use cpumask_copy instead of = Yinghai Lu
2009-06-09  6:57                     ` Rusty Russell [this message]
2009-06-09  8:13                       ` Yinghai Lu
2009-06-10  4:20                         ` Rusty Russell
2009-06-10 13:39                           ` Dave Jones
2009-06-10 17:01                             ` Ingo Molnar
2009-06-09 15:46                       ` Linus Torvalds
2009-06-09 16:28                         ` Dave Jones
2009-06-09 16:41                           ` Linus Torvalds
2009-06-10  4:55                             ` Rusty Russell
2009-06-10  6:22                         ` Rusty Russell
2009-06-10 11:10                           ` S06cpuspeed/2637 is trying to acquire lock (&(&dbs_info->work)->work (was: Re: [PATCH 4/6] x86/cpufreq: use cpumask_copy instead of =) Ingo Molnar
2009-06-10 20:58                             ` Dave Jones
2009-06-11 10:52                               ` Ingo Molnar
2009-06-20 12:48                                 ` Ingo Molnar
2009-06-21 19:55                                   ` Thomas Renninger
2009-06-23 18:17                                     ` [PATCH] cpufreq: remove dbs_mutex Ingo Molnar
2009-06-23 18:40                                       ` Ingo Molnar
2009-06-23 18:51                                         ` Pallipadi, Venkatesh
2009-06-23 19:14                                           ` Ingo Molnar
2009-06-23 19:24                                             ` Pallipadi, Venkatesh
2009-06-23 19:32                                               ` Ingo Molnar
2009-06-25 14:01                                                 ` Fix dead lock in cpufreq for CPU hotplug and suspend for 2.6.30.stable Thomas Renninger
2009-06-25 14:06                                                   ` Thomas Renninger
2009-06-25 14:01                                                 ` [PATCH 1/2] CPUFREQ: Remove unneeded dbs_mutexes from ondemand and conservative governors Thomas Renninger
2009-06-25 14:25                                                   ` Mathieu Desnoyers
2009-06-25 15:03                                                     ` Pallipadi, Venkatesh
2009-06-25 22:17                                                     ` Thomas Renninger
2009-06-25 22:26                                                       ` Thomas Renninger
2009-06-30  6:33                                                   ` Pavel Machek
2009-07-03 10:10                                                     ` Thomas Renninger
2009-07-05 19:46                                                       ` Pavel Machek
2009-06-30 22:58                                                   ` [stable] " Greg KH
2009-06-30 23:14                                                     ` Mathieu Desnoyers
2009-06-30 23:39                                                       ` Greg KH
2009-07-01  9:07                                                         ` Thomas Renninger
2009-06-25 14:01                                                 ` [PATCH 2/2] remove rwsem lock from CPUFREQ_GOV_STOP call (second call site) Thomas Renninger
2009-06-10 19:42                           ` [PATCH 4/6] x86/cpufreq: use cpumask_copy instead of = Langsdorf, Mark
2009-06-11  2:34                             ` Rusty Russell
2009-09-21 16:44                               ` Langsdorf, Mark
2009-06-06 21:55                   ` [PATCH 5/6] core: use cpumask_copy instead of = for cpus_allowed in fork Yinghai Lu
2009-06-06 21:56                   ` [PATCH 6/6] x86/cpufreq: don't use SPEEDSTEP with MAXSMP Yinghai Lu
2009-06-06 21:56                   ` [PATCH 1/6] cpumask: introduce zalloc_cpumask_var Andrew Morton
2009-06-06 22:07                     ` Yinghai Lu
2009-06-06 21:58                   ` Linus Torvalds

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=200906091627.45411.rusty@rustcorp.com.au \
    --to=rusty@rustcorp.com.au \
    --cc=akpm@linux-foundation.org \
    --cc=avi@redhat.com \
    --cc=cpufreq@vger.kernel.org \
    --cc=davej@redhat.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=yinghai@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