Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1622807

[patch V2 06/13] sparc/sysfs: Replace racy task affinity logic

From Thomas Gleixner <tglx@linutronix.de>
Newsgroups linux.kernel
Subject [patch V2 06/13] sparc/sysfs: Replace racy task affinity logic
Date 2017-04-13 10:20 +0200
Message-ID <tvI8W-cb-3@gated-at.bofh.it> (permalink)
References <tvx3P-QV-3@gated-at.bofh.it> <tvx3Q-QV-45@gated-at.bofh.it> <tvCdb-4gk-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


The mmustat_enable sysfs file accessor functions must run code on the
target CPU. This is achieved by temporarily setting the affinity of the
calling user space thread to the requested CPU and reset it to the original
affinity afterwards.

That's racy vs. concurrent affinity settings for that thread resulting in
code executing on the wrong CPU and overwriting the new affinity setting.

Replace it by using work_on_cpu() which guarantees to run the code on the
requested CPU.

Protection against CPU hotplug is not required as the open sysfs file
already prevents the removal from the CPU offline callback. Using the
hotplug protected version would actually be wrong because it would deadlock
against a CPU hotplug operation of the CPU associated to the sysfs file in
progress.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: David S. Miller <davem@davemloft.net>
Cc: sparclinux@vger.kernel.org
---

V2: Use NULL instead of 0 (David), fix compile breakage (build-bot)

 arch/sparc/kernel/sysfs.c |   39 +++++++++++----------------------------
 1 file changed, 11 insertions(+), 28 deletions(-)

Index: b/arch/sparc/kernel/sysfs.c
===================================================================
--- a/arch/sparc/kernel/sysfs.c
+++ b/arch/sparc/kernel/sysfs.c
@@ -98,27 +98,7 @@ static struct attribute_group mmu_stat_g
 	.name = "mmu_stats",
 };
 
-/* XXX convert to rusty's on_one_cpu */
-static unsigned long run_on_cpu(unsigned long cpu,
-			        unsigned long (*func)(unsigned long),
-				unsigned long arg)
-{
-	cpumask_t old_affinity;
-	unsigned long ret;
-
-	cpumask_copy(&old_affinity, &current->cpus_allowed);
-	/* should return -EINVAL to userspace */
-	if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
-		return 0;
-
-	ret = func(arg);
-
-	set_cpus_allowed_ptr(current, &old_affinity);
-
-	return ret;
-}
-
-static unsigned long read_mmustat_enable(unsigned long junk)
+static long read_mmustat_enable(void *data __maybe_unused)
 {
 	unsigned long ra = 0;
 
@@ -127,11 +107,11 @@ static unsigned long read_mmustat_enable
 	return ra != 0;
 }
 
-static unsigned long write_mmustat_enable(unsigned long val)
+static long write_mmustat_enable(void *data)
 {
-	unsigned long ra, orig_ra;
+	unsigned long ra, orig_ra, *val = data;
 
-	if (val)
+	if (*val)
 		ra = __pa(&per_cpu(mmu_stats, smp_processor_id()));
 	else
 		ra = 0UL;
@@ -142,7 +122,8 @@ static unsigned long write_mmustat_enabl
 static ssize_t show_mmustat_enable(struct device *s,
 				struct device_attribute *attr, char *buf)
 {
-	unsigned long val = run_on_cpu(s->id, read_mmustat_enable, 0);
+	long val = work_on_cpu(s->id, read_mmustat_enable, NULL);
+
 	return sprintf(buf, "%lx\n", val);
 }
 
@@ -150,13 +131,15 @@ static ssize_t store_mmustat_enable(stru
 			struct device_attribute *attr, const char *buf,
 			size_t count)
 {
-	unsigned long val, err;
-	int ret = sscanf(buf, "%lu", &val);
+	unsigned long val;
+	long err;
+	int ret;
 
+	ret = sscanf(buf, "%lu", &val);
 	if (ret != 1)
 		return -EINVAL;
 
-	err = run_on_cpu(s->id, write_mmustat_enable, val);
+	err = work_on_cpu(s->id, write_mmustat_enable, &val);
 	if (err)
 		return -EIO;
 

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[patch 00/13] sched/treewide: Clean up various racy task affinity  issues Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
  [patch 12/13] cpufreq/sparc-us2e: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 12/13] cpufreq/sparc-us2e: Replace racy task affinity  logic Viresh Kumar <viresh.kumar@linaro.org> - 2017-04-13 05:00 +0200
    [patch V2 12/13] cpufreq/sparc-us2e: Replace racy task affinity  logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 10:20 +0200
      [patch V3 12/13] cpufreq/sparc-us2e: Replace racy task affinity  logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 10:30 +0200
        [tip:sched/core] cpufreq/sparc-us2e: Replace racy task affinity  logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
      Re: [patch V2 12/13] cpufreq/sparc-us2e: Replace racy task  affinity logic David Miller <davem@davemloft.net> - 2017-04-13 17:00 +0200
  [patch 03/13] ia64/salinfo: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    [tip:sched/core] ia64/salinfo: Replace racy task affinity logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 04/13] ia64/sn/hwperf: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    [patch V 2 04/13] ia64/sn/hwperf: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 23:00 +0200
      [tip:sched/core] ia64/sn/hwperf: Replace racy task affinity logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 11/13] cpufreq/sparc-us3: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 11/13] cpufreq/sparc-us3: Replace racy task affinity logic Viresh Kumar <viresh.kumar@linaro.org> - 2017-04-13 04:50 +0200
    [tip:sched/core] cpufreq/sparc-us3: Replace racy task affinity  logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 01/13] ia64/topology: Remove cpus_allowed manipulation Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    [tip:sched/core] ia64/topology: Remove cpus_allowed manipulation tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 07/13] ACPI/processor: Fix error handling in  __acpi_processor_start() Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    [tip:sched/core] ACPI/processor: Fix error handling in  __acpi_processor_start() tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 08/13] ACPI/processor: Replace racy task affinity logic. Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 08/13] ACPI/processor: Replace racy task affinity logic. Peter Zijlstra <peterz@infradead.org> - 2017-04-13 13:50 +0200
      Re: [patch 08/13] ACPI/processor: Replace racy task affinity  logic. Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 14:10 +0200
        Re: [patch 08/13] ACPI/processor: Replace racy task affinity logic. Peter Zijlstra <peterz@infradead.org> - 2017-04-13 15:00 +0200
    [tip:sched/core] ACPI/processor: Replace racy task affinity logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 13/13] crypto: n2 - Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 13/13] crypto: n2 - Replace racy task affinity logic Herbert Xu <herbert@gondor.apana.org.au> - 2017-04-13 07:40 +0200
    [patch V2 13/13] crypto: n2 - Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 10:30 +0200
      Re: [patch V2 13/13] crypto: n2 - Replace racy task affinity logic David Miller <davem@davemloft.net> - 2017-04-13 17:00 +0200
      [tip:sched/core] crypto: N2 - Replace racy task affinity logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:40 +0200
  [patch 05/13] powerpc/smp: Replace open coded task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 05/13] powerpc/smp: Replace open coded task affinity logic Michael Ellerman <mpe@ellerman.id.au> - 2017-04-13 07:50 +0200
    [tip:sched/core] powerpc/smp: Replace open coded task affinity  logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  [patch 06/13] sparc/sysfs: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 06/13] sparc/sysfs: Replace racy task affinity logic David Miller <davem@davemloft.net> - 2017-04-13 04:00 +0200
      [patch V2 06/13] sparc/sysfs: Replace racy task affinity logic Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 10:20 +0200
        [tip:sched/core] sparc/sysfs: Replace racy task affinity logic tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:20 +0200
  [patch 02/13] workqueue: Provide work_on_cpu_safe() Thomas Gleixner <tglx@linutronix.de> - 2017-04-12 22:30 +0200
    Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Dou Liyang <douly.fnst@cn.fujitsu.com> - 2017-04-13 13:20 +0200
      Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Thomas Gleixner <tglx@linutronix.de> - 2017-04-13 23:30 +0200
    Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Tejun Heo <tj@kernel.org> - 2017-04-14 06:20 +0200
    Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Peter Zijlstra <peterz@infradead.org> - 2017-04-14 11:00 +0200
      Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Thomas Gleixner <tglx@linutronix.de> - 2017-04-14 12:00 +0200
        Re: [patch 02/13] workqueue: Provide work_on_cpu_safe() Peter Zijlstra <peterz@infradead.org> - 2017-04-14 12:00 +0200
    [tip:sched/core] workqueue: Provide work_on_cpu_safe() tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-04-15 16:30 +0200
  Re: [patch 00/13] sched/treewide: Clean up various racy task  affinity issues Peter Zijlstra <peterz@infradead.org> - 2017-04-13 11:10 +0200

csiph-web