aboutsummaryrefslogtreecommitdiffstats
path: root/xen/include
diff options
context:
space:
mode:
authorJan Beulich <jbeulich@suse.com>2013-08-23 15:01:53 +0200
committerJan Beulich <jbeulich@suse.com>2013-08-23 15:01:53 +0200
commit829b8498cc781f5ed8f7b9e16378f448b4d45213 (patch)
treefa6057dc0bf1586818a82815a0fff3243a969f21 /xen/include
parent3829655bd3ad2b1150bd94955fc6988dec6b98f2 (diff)
downloadxen-829b8498cc781f5ed8f7b9e16378f448b4d45213.tar.gz
xen-829b8498cc781f5ed8f7b9e16378f448b4d45213.tar.bz2
xen-829b8498cc781f5ed8f7b9e16378f448b4d45213.zip
un-alias cpumask_any() from cpumask_first()
In order to achieve more symmetric distribution of certain things, cpumask_any() shouldn't always pick the first CPU (which frequently will end up being CPU0). To facilitate that, introduce a library-like function to obtain random numbers. The per-architecture function is supposed to return zero if no valid random number can be obtained (implying that if occasionally zero got produced as random number, it wouldn't be considered such). As fallback this uses the trivial algorithm from the C standard, extended to produce "unsigned int" results. Signed-off-by: Jan Beulich <jbeulich@suse.com> Acked-by: Keir Fraser <keir@xen.org> Reviewed-by: George Dunlap <george.dunlap@eu.citrix.com>
Diffstat (limited to 'xen/include')
-rw-r--r--xen/include/asm-arm/percpu.h11
-rw-r--r--xen/include/asm-arm/random.h9
-rw-r--r--xen/include/asm-x86/random.h16
-rw-r--r--xen/include/xen/cpumask.h19
-rw-r--r--xen/include/xen/random.h6
5 files changed, 60 insertions, 1 deletions
diff --git a/xen/include/asm-arm/percpu.h b/xen/include/asm-arm/percpu.h
index a0c38cebf4..71e7649f7d 100644
--- a/xen/include/asm-arm/percpu.h
+++ b/xen/include/asm-arm/percpu.h
@@ -2,6 +2,17 @@
#define __ARM_PERCPU_H__
#ifndef __ASSEMBLY__
+
+#include <xen/types.h>
+#include <asm/cpregs.h>
+#if defined(CONFIG_ARM_32)
+# include <asm/arm32/processor.h>
+#elif defined(CONFIG_ARM_64)
+# include <asm/arm64/processor.h>
+#else
+# error "unknown ARM variant"
+#endif
+
extern char __per_cpu_start[], __per_cpu_data_end[];
extern unsigned long __per_cpu_offset[NR_CPUS];
void percpu_init_areas(void);
diff --git a/xen/include/asm-arm/random.h b/xen/include/asm-arm/random.h
new file mode 100644
index 0000000000..b4acee276b
--- /dev/null
+++ b/xen/include/asm-arm/random.h
@@ -0,0 +1,9 @@
+#ifndef __ASM_RANDOM_H__
+#define __ASM_RANDOM_H__
+
+static inline unsigned int arch_get_random(void)
+{
+ return 0;
+}
+
+#endif /* __ASM_RANDOM_H__ */
diff --git a/xen/include/asm-x86/random.h b/xen/include/asm-x86/random.h
new file mode 100644
index 0000000000..6198e72a7b
--- /dev/null
+++ b/xen/include/asm-x86/random.h
@@ -0,0 +1,16 @@
+#ifndef __ASM_RANDOM_H__
+#define __ASM_RANDOM_H__
+
+#include <asm/processor.h>
+
+static inline unsigned int arch_get_random(void)
+{
+ unsigned int val = 0;
+
+ if ( cpu_has(&current_cpu_data, X86_FEATURE_RDRAND) )
+ asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) );
+
+ return val;
+}
+
+#endif /* __ASM_RANDOM_H__ */
diff --git a/xen/include/xen/cpumask.h b/xen/include/xen/cpumask.h
index f931cf2618..850b4a22d7 100644
--- a/xen/include/xen/cpumask.h
+++ b/xen/include/xen/cpumask.h
@@ -77,6 +77,7 @@
#include <xen/bitmap.h>
#include <xen/kernel.h>
+#include <xen/random.h>
typedef struct cpumask{ DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
@@ -245,7 +246,23 @@ static inline int cpumask_cycle(int n, const cpumask_t *srcp)
return nxt;
}
-#define cpumask_any(srcp) cpumask_first(srcp)
+static inline unsigned int cpumask_any(const cpumask_t *srcp)
+{
+ unsigned int cpu = cpumask_first(srcp);
+ unsigned int w = cpumask_weight(srcp);
+
+ if ( w > 1 && cpu < nr_cpu_ids )
+ for ( w = get_random() % w; w--; )
+ {
+ unsigned int next = cpumask_next(cpu, srcp);
+
+ if ( next >= nr_cpu_ids )
+ break;
+ cpu = next;
+ }
+
+ return cpu;
+}
/*
* Special-case data structure for "single bit set only" constant CPU masks.
diff --git a/xen/include/xen/random.h b/xen/include/xen/random.h
new file mode 100644
index 0000000000..7c43d87f59
--- /dev/null
+++ b/xen/include/xen/random.h
@@ -0,0 +1,6 @@
+#ifndef __XEN_RANDOM_H__
+#define __XEN_RANDOM_H__
+
+unsigned int get_random(void);
+
+#endif /* __XEN_RANDOM_H__ */