From: "Jan Beulich" Subject: [PATCH 1/5] un-alias cpumask_any() from cpumask_first() Date: Fri, 23 Aug 2013 08:52:46 +0100 Message-ID: <521730EE02000078000EDE88@nat28.tlf.novell.com> References: <5217309002000078000EDE7D@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartE7D4AFDE.0__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VCmAs-0007pN-Mi for xen-devel@lists.xenproject.org; Fri, 23 Aug 2013 07:52:50 +0000 In-Reply-To: <5217309002000078000EDE7D@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartE7D4AFDE.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -17,6 +17,7 @@ obj-y +=3D multicall.o obj-y +=3D notifier.o obj-y +=3D page_alloc.o obj-y +=3D preempt.o +obj-y +=3D random.o obj-y +=3D rangeset.o obj-y +=3D sched_credit.o obj-y +=3D sched_credit2.o --- /dev/null +++ b/xen/common/random.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +static DEFINE_PER_CPU(unsigned int, seed); + +unsigned int get_random(void) +{ + unsigned int next =3D this_cpu(seed), val =3D arch_get_random(); + + if ( unlikely(!next) ) + next =3D val ?: NOW(); + + if ( !val ) + { + unsigned int i; + + for ( i =3D 0; i < sizeof(val) * 8; i +=3D 11 ) + { + next =3D next * 1103515245 + 12345; + val |=3D ((next >> 16) & 0x7ff) << i; + } + } + + this_cpu(seed) =3D next; + + return val; +} --- a/xen/include/asm-arm/percpu.h +++ b/xen/include/asm-arm/percpu.h @@ -2,6 +2,17 @@ #define __ARM_PERCPU_H__ =20 #ifndef __ASSEMBLY__ + +#include +#include +#if defined(CONFIG_ARM_32) +# include +#elif defined(CONFIG_ARM_64) +# include +#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); --- /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__ */ --- /dev/null +++ b/xen/include/asm-x86/random.h @@ -0,0 +1,16 @@ +#ifndef __ASM_RANDOM_H__ +#define __ASM_RANDOM_H__ + +#include + +static inline unsigned int arch_get_random(void) +{ + unsigned int val =3D 0; + + if ( cpu_has(¤t_cpu_data, X86_FEATURE_RDRAND) ) + asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) ); + + return val; +} + +#endif /* __ASM_RANDOM_H__ */ --- a/xen/include/xen/cpumask.h +++ b/xen/include/xen/cpumask.h @@ -77,6 +77,7 @@ =20 #include #include +#include =20 typedef struct cpumask{ DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; =20 @@ -245,7 +246,23 @@ static inline int cpumask_cycle(int n, c return nxt; } =20 -#define cpumask_any(srcp) cpumask_first(srcp) +static inline unsigned int cpumask_any(const cpumask_t *srcp) +{ + unsigned int cpu =3D cpumask_first(srcp); + unsigned int w =3D cpumask_weight(srcp); + + if ( w > 1 && cpu < nr_cpu_ids ) + for ( w =3D get_random() % w; w--; ) + { + unsigned int next =3D cpumask_next(cpu, srcp); + + if ( next >=3D nr_cpu_ids ) + break; + cpu =3D next; + } + + return cpu; +} =20 /* * Special-case data structure for "single bit set only" constant CPU = masks. --- /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__ */ --=__PartE7D4AFDE.0__= Content-Type: text/plain; name="random.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="random.patch" un-alias cpumask_any() from cpumask_first()=0A=0AIn order to achieve more = symmetric distribution of certain things,=0Acpumask_any() shouldn't always = pick the first CPU (which frequently=0Awill end up being CPU0). To = facilitate that, introduce a library-like=0Afunction to obtain random = numbers.=0A=0AThe per-architecture function is supposed to return zero if = no valid=0Arandom number can be obtained (implying that if occasionally = zero got=0Aproduced as random number, it wouldn't be considered = such).=0A=0AAs fallback this uses the trivial algorithm from the C = standard,=0Aextended to produce "unsigned int" results.=0A=0ASigned-off-by:= Jan Beulich =0A=0A--- a/xen/common/Makefile=0A+++ = b/xen/common/Makefile=0A@@ -17,6 +17,7 @@ obj-y +=3D multicall.o=0A obj-y = +=3D notifier.o=0A obj-y +=3D page_alloc.o=0A obj-y +=3D preempt.o=0A+obj-y= +=3D random.o=0A obj-y +=3D rangeset.o=0A obj-y +=3D sched_credit.o=0A = obj-y +=3D sched_credit2.o=0A--- /dev/null=0A+++ b/xen/common/random.c=0A@@= -0,0 +1,29 @@=0A+#include =0A+#include =0A+#in= clude =0A+#include =0A+=0A+static DEFINE_PER_CPU(= unsigned int, seed);=0A+=0A+unsigned int get_random(void)=0A+{=0A+ = unsigned int next =3D this_cpu(seed), val =3D arch_get_random();=0A+=0A+ = if ( unlikely(!next) )=0A+ next =3D val ?: NOW();=0A+=0A+ if ( = !val )=0A+ {=0A+ unsigned int i;=0A+=0A+ for ( i =3D 0; i = < sizeof(val) * 8; i +=3D 11 )=0A+ {=0A+ next =3D next * = 1103515245 + 12345;=0A+ val |=3D ((next >> 16) & 0x7ff) << = i;=0A+ }=0A+ }=0A+=0A+ this_cpu(seed) =3D next;=0A+=0A+ = return val;=0A+}=0A--- a/xen/include/asm-arm/percpu.h=0A+++ b/xen/include/a= sm-arm/percpu.h=0A@@ -2,6 +2,17 @@=0A #define __ARM_PERCPU_H__=0A =0A = #ifndef __ASSEMBLY__=0A+=0A+#include =0A+#include =0A+#if defined(CONFIG_ARM_32)=0A+# include =0A+#e= lif defined(CONFIG_ARM_64)=0A+# include =0A+#else=0A= +# error "unknown ARM variant"=0A+#endif=0A+=0A extern char __per_cpu_start= [], __per_cpu_data_end[];=0A extern unsigned long __per_cpu_offset[NR_CPUS]= ;=0A void percpu_init_areas(void);=0A--- /dev/null=0A+++ b/xen/include/asm-= arm/random.h=0A@@ -0,0 +1,9 @@=0A+#ifndef __ASM_RANDOM_H__=0A+#define = __ASM_RANDOM_H__=0A+=0A+static inline unsigned int arch_get_random(void)=0A= +{=0A+ return 0;=0A+}=0A+=0A+#endif /* __ASM_RANDOM_H__ */=0A--- = /dev/null=0A+++ b/xen/include/asm-x86/random.h=0A@@ -0,0 +1,16 @@=0A+#ifnde= f __ASM_RANDOM_H__=0A+#define __ASM_RANDOM_H__=0A+=0A+#include =0A+=0A+static inline unsigned int arch_get_random(void)=0A+{=0A+ = unsigned int val =3D 0;=0A+=0A+ if ( cpu_has(¤t_cpu_data, = X86_FEATURE_RDRAND) )=0A+ asm ( ".byte 0x0f,0xc7,0xf0" : "+a" (val) = );=0A+=0A+ return val;=0A+}=0A+=0A+#endif /* __ASM_RANDOM_H__ */=0A--- = a/xen/include/xen/cpumask.h=0A+++ b/xen/include/xen/cpumask.h=0A@@ -77,6 = +77,7 @@=0A =0A #include =0A #include =0A+#incl= ude =0A =0A typedef struct cpumask{ DECLARE_BITMAP(bits, = NR_CPUS); } cpumask_t;=0A =0A@@ -245,7 +246,23 @@ static inline int = cpumask_cycle(int n, c=0A return nxt;=0A }=0A =0A-#define cpumask_any(s= rcp) cpumask_first(srcp)=0A+static inline unsigned int cpumask_any(const = cpumask_t *srcp)=0A+{=0A+ unsigned int cpu =3D cpumask_first(srcp);=0A+ = unsigned int w =3D cpumask_weight(srcp);=0A+=0A+ if ( w > 1 && cpu < = nr_cpu_ids )=0A+ for ( w =3D get_random() % w; w--; )=0A+ = {=0A+ unsigned int next =3D cpumask_next(cpu, srcp);=0A+=0A+ = if ( next >=3D nr_cpu_ids )=0A+ break;=0A+ = cpu =3D next;=0A+ }=0A+=0A+ return cpu;=0A+}=0A =0A /*=0A * = Special-case data structure for "single bit set only" constant CPU = masks.=0A--- /dev/null=0A+++ b/xen/include/xen/random.h=0A@@ -0,0 +1,6 = @@=0A+#ifndef __XEN_RANDOM_H__=0A+#define __XEN_RANDOM_H__=0A+=0A+unsigned = int get_random(void);=0A+=0A+#endif /* __XEN_RANDOM_H__ */=0A --=__PartE7D4AFDE.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartE7D4AFDE.0__=--