2022-06-23 15:55:55

by Guo Hui

[permalink] [raw]
Subject: [PATCH] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the parameters of
the vcpu_is_preempted function in the X86 architecture
physical machine are redundant instructions, which cause the
multi-core performance of Unixbench to drop by
about 300 to 500 points. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter assignments in the function osq_lock
that call the function vcpu_is_preempted are as follows:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch implements the replacement of the above instructions with
the nop instruction at system startup.
When the assignment C code changes,
the above assignment instructions may also change accordingly.
In order to flexibly replace the instructions generated by the C code,
this patch defines the macro ALTERNATIVE_C_CODE
from the ALTERNATIVE macro. Different from the first parameter
of the ALTERNATIVE macro, the ALTERNATIVE_C_CODE macro is
the first parameter is a C code statement and cannot contain
a return statement. To use the macro, for example,
replace the instructions generated by the
C code 'cpu = node->cpu - 1;' with the nop instruction:

OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 944608003.9 80943.3
Double-Precision Whetstone 55.0 212045.8 38553.8
Execl Throughput 43.0 42772.3 9947.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 462656.6 1168.3
File Copy 256 bufsize 500 maxblocks 1655.0 120043.6 725.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1544525.5 2663.0
Pipe Throughput 12440.0 47277698.5 38004.6
Pipe-based Context Switching 4000.0 1894556.8 4736.4
Process Creation 126.0 86077.0 6831.5
Shell Scripts (1 concurrent) 42.4 70236.3 16565.2
Shell Scripts (8 concurrent) 6.0 8978.1 14963.4
System Call Overhead 15000.0 4691260.0 3127.5
========
System Benchmarks Index Score 7980.9

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 312233.0 788.5
File Copy 256 bufsize 500 maxblocks 1655.0 80050.9 483.7
File Copy 4096 bufsize 8000 maxblocks 5800.0 1036101.7 1786.4
Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7
========
System Benchmarks Index Score 8260.6

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256283941.6 193340.5
Double-Precision Whetstone 55.0 439577.3 79923.2
Execl Throughput 43.0 10013.6 2328.7
File Copy 1024 bufsize 2000 maxblocks 3960.0 278121.5 702.3
File Copy 256 bufsize 500 maxblocks 1655.0 71835.5 434.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 905654.2 1561.5
Pipe Throughput 12440.0 117715166.2 94626.3
Pipe-based Context Switching 4000.0 7731331.7 19328.3
Process Creation 126.0 30157.8 2393.5
Shell Scripts (1 concurrent) 42.4 48670.8 11479.0
Shell Scripts (8 concurrent) 6.0 6595.6 10992.7
System Call Overhead 15000.0 6766475.9 4511.0
========
System Benchmarks Index Score 7688.7

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1
File Copy 1024 bufsize 2000 maxblocks 3960.0 312233.0 788.5
File Copy 256 bufsize 500 maxblocks 1655.0 80050.9 483.7
File Copy 4096 bufsize 8000 maxblocks 5800.0 1036101.7 1786.4
Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7
========
System Benchmarks Index Score 8260.6

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/include/asm/alternative.h | 15 +++++++++++++++
arch/x86/kernel/alternative.c | 11 +++++++++++
include/linux/osq_lock.h | 7 +++++++
kernel/locking/osq_lock.c | 6 +++++-
4 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index 9b10c8c76..5979ebe89 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -167,6 +167,21 @@ static inline int alternatives_text_reserved(void *start, void *end)
ALTINSTR_REPLACEMENT(newinstr, 1) \
".popsection\n"

+/* alternative c code primitive: */
+#define ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
+do { \
+ asm volatile("661:\n\t"); \
+ oldinstr; \
+ asm volatile("\n662:\n" \
+ alt_end_marker ":\n" \
+ ".pushsection .altinstructions,\"a\"\n" \
+ ALTINSTR_ENTRY(feature, 1) \
+ ".popsection\n" \
+ ".pushsection .altinstr_replacement, \"ax\"\n" \
+ ALTINSTR_REPLACEMENT(newinstr, 1) \
+ ".popsection\n"); \
+} while (0)
+
#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
OLDINSTR_2(oldinstr, 1, 2) \
".pushsection .altinstructions,\"a\"\n" \
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index e257f6c80..cf77be884 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -251,6 +251,8 @@ static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
}
}

+extern bool pv_is_native_vcpu_is_preempted(void);
+
/*
* Replace instructions with better alternatives for this CPU type. This runs
* before SMP is initialized to avoid SMP problems with self modifying code.
@@ -285,6 +287,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,

instr = (u8 *)&a->instr_offset + a->instr_offset;
replacement = (u8 *)&a->repl_offset + a->repl_offset;
+
+ if (*replacement == 0x90 && a->replacementlen == 1) {
+#if defined(CONFIG_PARAVIRT_SPINLOCKS)
+ if (pv_is_native_vcpu_is_preempted())
+ add_nops(instr, a->instrlen);
+#endif
+ continue;
+ }
+
BUG_ON(a->instrlen > sizeof(insn_buff));
BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);

diff --git a/include/linux/osq_lock.h b/include/linux/osq_lock.h
index 5581dbd3b..ee960e3aa 100644
--- a/include/linux/osq_lock.h
+++ b/include/linux/osq_lock.h
@@ -38,4 +38,11 @@ static inline bool osq_is_locked(struct optimistic_spin_queue *lock)
return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
}

+#ifdef ALTERNATIVE_C_CODE
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
+ ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)
+#else
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) oldinstr
+#endif
+
#endif
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad52..bbe7d640c 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -24,7 +24,11 @@ static inline int encode_cpu(int cpu_nr)

static inline int node_cpu(struct optimistic_spin_node *node)
{
- return node->cpu - 1;
+ int cpu = 0;
+
+ OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);
+
+ return cpu;
}

static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
--
2.20.1




2022-06-23 21:57:16

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 6/23/22 11:50, Guo Hui wrote:
> The instructions assigned to the parameters of
> the vcpu_is_preempted function in the X86 architecture
> physical machine are redundant instructions, which cause the
> multi-core performance of Unixbench to drop by
> about 300 to 500 points. The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter assignments in the function osq_lock
> that call the function vcpu_is_preempted are as follows:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch implements the replacement of the above instructions with
> the nop instruction at system startup.
> When the assignment C code changes,
> the above assignment instructions may also change accordingly.
> In order to flexibly replace the instructions generated by the C code,
> this patch defines the macro ALTERNATIVE_C_CODE
> from the ALTERNATIVE macro. Different from the first parameter
> of the ALTERNATIVE macro, the ALTERNATIVE_C_CODE macro is
> the first parameter is a C code statement and cannot contain
> a return statement. To use the macro, for example,
> replace the instructions generated by the
> C code 'cpu = node->cpu - 1;' with the nop instruction:
>
> OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
>
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 944608003.9 80943.3
> Double-Precision Whetstone 55.0 212045.8 38553.8
> Execl Throughput 43.0 42772.3 9947.1
> File Copy 1024 bufsize 2000 maxblocks 3960.0 462656.6 1168.3
> File Copy 256 bufsize 500 maxblocks 1655.0 120043.6 725.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1544525.5 2663.0
> Pipe Throughput 12440.0 47277698.5 38004.6
> Pipe-based Context Switching 4000.0 1894556.8 4736.4
> Process Creation 126.0 86077.0 6831.5
> Shell Scripts (1 concurrent) 42.4 70236.3 16565.2
> Shell Scripts (8 concurrent) 6.0 8978.1 14963.4
> System Call Overhead 15000.0 4691260.0 3127.5
> ========
> System Benchmarks Index Score 7980.9
>
> After using the patch:
>
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
> Double-Precision Whetstone 55.0 438940.3 79807.3
> Execl Throughput 43.0 10720.3 2493.1
> File Copy 1024 bufsize 2000 maxblocks 3960.0 312233.0 788.5
> File Copy 256 bufsize 500 maxblocks 1655.0 80050.9 483.7
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1036101.7 1786.4
> Pipe Throughput 12440.0 117700315.3 94614.4
> Pipe-based Context Switching 4000.0 8421909.8 21054.8
> Process Creation 126.0 36742.0 2916.0
> Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
> Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
> System Call Overhead 15000.0 6791548.2 4527.7
> ========
> System Benchmarks Index Score 8260.6
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
>
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256283941.6 193340.5
> Double-Precision Whetstone 55.0 439577.3 79923.2
> Execl Throughput 43.0 10013.6 2328.7
> File Copy 1024 bufsize 2000 maxblocks 3960.0 278121.5 702.3
> File Copy 256 bufsize 500 maxblocks 1655.0 71835.5 434.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 905654.2 1561.5
> Pipe Throughput 12440.0 117715166.2 94626.3
> Pipe-based Context Switching 4000.0 7731331.7 19328.3
> Process Creation 126.0 30157.8 2393.5
> Shell Scripts (1 concurrent) 42.4 48670.8 11479.0
> Shell Scripts (8 concurrent) 6.0 6595.6 10992.7
> System Call Overhead 15000.0 6766475.9 4511.0
> ========
> System Benchmarks Index Score 7688.7
>
> After using the patch:
>
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
> Double-Precision Whetstone 55.0 438940.3 79807.3
> Execl Throughput 43.0 10720.3 2493.1
> File Copy 1024 bufsize 2000 maxblocks 3960.0 312233.0 788.5
> File Copy 256 bufsize 500 maxblocks 1655.0 80050.9 483.7
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1036101.7 1786.4
> Pipe Throughput 12440.0 117700315.3 94614.4
> Pipe-based Context Switching 4000.0 8421909.8 21054.8
> Process Creation 126.0 36742.0 2916.0
> Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
> Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
> System Call Overhead 15000.0 6791548.2 4527.7
> ========
> System Benchmarks Index Score 8260.6
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/include/asm/alternative.h | 15 +++++++++++++++
> arch/x86/kernel/alternative.c | 11 +++++++++++
> include/linux/osq_lock.h | 7 +++++++
> kernel/locking/osq_lock.c | 6 +++++-
> 4 files changed, 38 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
> index 9b10c8c76..5979ebe89 100644
> --- a/arch/x86/include/asm/alternative.h
> +++ b/arch/x86/include/asm/alternative.h
> @@ -167,6 +167,21 @@ static inline int alternatives_text_reserved(void *start, void *end)
> ALTINSTR_REPLACEMENT(newinstr, 1) \
> ".popsection\n"
>
> +/* alternative c code primitive: */
> +#define ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
> +do { \
> + asm volatile("661:\n\t"); \
> + oldinstr; \
> + asm volatile("\n662:\n" \
> + alt_end_marker ":\n" \
> + ".pushsection .altinstructions,\"a\"\n" \
> + ALTINSTR_ENTRY(feature, 1) \
> + ".popsection\n" \
> + ".pushsection .altinstr_replacement, \"ax\"\n" \
> + ALTINSTR_REPLACEMENT(newinstr, 1) \
> + ".popsection\n"); \
> +} while (0)
> +
> #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
> OLDINSTR_2(oldinstr, 1, 2) \
> ".pushsection .altinstructions,\"a\"\n" \
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index e257f6c80..cf77be884 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -251,6 +251,8 @@ static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
> }
> }
>
> +extern bool pv_is_native_vcpu_is_preempted(void);
> +
> /*
> * Replace instructions with better alternatives for this CPU type. This runs
> * before SMP is initialized to avoid SMP problems with self modifying code.
> @@ -285,6 +287,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
>
> instr = (u8 *)&a->instr_offset + a->instr_offset;
> replacement = (u8 *)&a->repl_offset + a->repl_offset;
> +
> + if (*replacement == 0x90 && a->replacementlen == 1) {
> +#if defined(CONFIG_PARAVIRT_SPINLOCKS)
> + if (pv_is_native_vcpu_is_preempted())
> + add_nops(instr, a->instrlen);
> +#endif
> + continue;
> + }
> +
This is hacky and it may incorrectly affect other alternatives that
patches thing to nop.
> BUG_ON(a->instrlen > sizeof(insn_buff));
> BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);
>
> diff --git a/include/linux/osq_lock.h b/include/linux/osq_lock.h
> index 5581dbd3b..ee960e3aa 100644
> --- a/include/linux/osq_lock.h
> +++ b/include/linux/osq_lock.h
> @@ -38,4 +38,11 @@ static inline bool osq_is_locked(struct optimistic_spin_queue *lock)
> return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
> }
>
> +#ifdef ALTERNATIVE_C_CODE
> +#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
> + ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)
> +#else
> +#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) oldinstr
> +#endif
> +
> #endif
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad52..bbe7d640c 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -24,7 +24,11 @@ static inline int encode_cpu(int cpu_nr)
>
> static inline int node_cpu(struct optimistic_spin_node *node)
> {
> - return node->cpu - 1;
> + int cpu = 0;
> +
> + OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);
> +
> + return cpu;
> }

Why don't you use static_key to control the alternative and add a
late_initcall() to set it. I think that will be simpler and you can
throw away ALTERNATIVE_C_CODE().

Cheers,
Longman

2022-06-27 02:20:42

by Guo Hui

[permalink] [raw]
Subject: [PATCH v2] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preemted_key);

static inline int node_cpu(struct optimistic_spin_node *node)
{
int cpu = 0;

if (!static_branch_unlikely(&preemted_key))
cpu = node->cpu - 1;

return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
kernel/locking/osq_lock.c | 9 ++++++++-
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99ad..7a55f8407 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_FALSE(preemted_key);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)

if (!pv_is_native_vcpu_is_preempted())
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ else
+ static_branch_enable(&preemted_key);
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad52..a8798e701 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
return cpu_nr + 1;
}

+DEFINE_STATIC_KEY_FALSE(preemted_key);
+
static inline int node_cpu(struct optimistic_spin_node *node)
{
- return node->cpu - 1;
+ int cpu = 0;
+
+ if (!static_branch_unlikely(&preemted_key))
+ cpu = node->cpu - 1;
+
+ return cpu;
}

static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
--
2.20.1



2022-06-27 03:13:48

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v2] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 6/26/22 22:13, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The code is as follows:
>
> DEFINE_STATIC_KEY_FALSE(preemted_key);
>
> static inline int node_cpu(struct optimistic_spin_node *node)
> {
> int cpu = 0;
>
> if (!static_branch_unlikely(&preemted_key))
> cpu = node->cpu - 1;
>
> return cpu;
> }
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 9 ++++++++-
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99ad..7a55f8407 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_FALSE(preemted_key);
> +
> void __init paravirt_set_cap(void)
> {
> if (!pv_is_native_spin_unlock())
> @@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
>
> if (!pv_is_native_vcpu_is_preempted())
> setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
> + else
> + static_branch_enable(&preemted_key);
> }
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad52..a8798e701 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
> return cpu_nr + 1;
> }
>
> +DEFINE_STATIC_KEY_FALSE(preemted_key);
> +
> static inline int node_cpu(struct optimistic_spin_node *node)
> {
> - return node->cpu - 1;
> + int cpu = 0;
> +
> + if (!static_branch_unlikely(&preemted_key))
> + cpu = node->cpu - 1;
> +
> + return cpu;
> }
>
> static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)

This looks much better now.

However vcpu_is_preempted() is not just used by x86, it is also used in
arm64, powerpc and s390. If you want to default disable node_cpu(), you
have to either make the necessary changes in all those places or you
have to change it a way that won't affect other arches while disabling
it for the x86 case.

Cheers,
Longman

2022-06-27 06:04:13

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH v2] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 27.06.22 04:13, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The code is as follows:
>
> DEFINE_STATIC_KEY_FALSE(preemted_key);
>
> static inline int node_cpu(struct optimistic_spin_node *node)
> {
> int cpu = 0;
>
> if (!static_branch_unlikely(&preemted_key))
> cpu = node->cpu - 1;
>
> return cpu;
> }
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 9 ++++++++-
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99ad..7a55f8407 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_FALSE(preemted_key);

Please name it "preempted_key".


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2022-06-27 07:33:23

by Guo Hui

[permalink] [raw]
Subject: [PATCH v3] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preempted_key);

static inline int node_cpu(struct optimistic_spin_node *node)
{
int cpu = 0;

if (!static_branch_unlikely(&preempted_key))
cpu = node->cpu - 1;

return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
kernel/locking/osq_lock.c | 9 ++++++++-
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99ad..7a55f8407 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_FALSE(preempted_key);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)

if (!pv_is_native_vcpu_is_preempted())
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ else
+ static_branch_enable(&preempted_key);
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad52..a8798e701 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
return cpu_nr + 1;
}

+DEFINE_STATIC_KEY_FALSE(preempted_key);
+
static inline int node_cpu(struct optimistic_spin_node *node)
{
- return node->cpu - 1;
+ int cpu = 0;
+
+ if (!static_branch_unlikely(&preempted_key))
+ cpu = node->cpu - 1;
+
+ return cpu;
}

static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
--
2.20.1



2022-06-27 08:01:51

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH v2] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On Mon, Jun 27, 2022 at 10:13:50AM +0800, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.

The above basically says that argument setup is not patched out and
causes significant pain due to a cache-miss.

> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 9 ++++++++-
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99ad..7a55f8407 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_FALSE(preemted_key);
> +
> void __init paravirt_set_cap(void)
> {
> if (!pv_is_native_spin_unlock())
> @@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
>
> if (!pv_is_native_vcpu_is_preempted())
> setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
> + else
> + static_branch_enable(&preemted_key);
> }

At least for x86 it makes sense to have the static_key default the other
way around. That is, enable it along with vcpu_is_preempted().

> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad52..a8798e701 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
> return cpu_nr + 1;
> }
>
> +DEFINE_STATIC_KEY_FALSE(preemted_key);
> +
> static inline int node_cpu(struct optimistic_spin_node *node)
> {
> - return node->cpu - 1;
> + int cpu = 0;
> +
> + if (!static_branch_unlikely(&preemted_key))
> + cpu = node->cpu - 1;
> +
> + return cpu;
> }

Would not something like:

static inline bool
vcpu_is_preempted_node(struct optimistic_spin_node *node)
{
if (!static_branch_unlikely(&vcpu_has_preemption))
return false;

return vcpu_is_preempted(node_cpu(node->prev));
}

And then use that like:

if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
vcpu_is_preempted_node(node)))

Not generate better code still?

2022-06-27 14:39:57

by Guo Hui

[permalink] [raw]
Subject: [PATCH v4] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
kernel/locking/osq_lock.c | 12 +++++++++++-
2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99ad..a2eb375e2 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)

if (!pv_is_native_vcpu_is_preempted())
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ else
+ static_branch_disable(&vcpu_has_preemption);
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad52..adb41080d 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
return node->cpu - 1;
}

+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+ if (!static_branch_unlikely(&vcpu_has_preemption))
+ return false;
+
+ return vcpu_is_preempted(node_cpu(node->prev));
+}
+
static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
{
int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* polling, be careful.
*/
if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+ vcpu_is_preempted_node(node)))
return true;

/* unqueue */
--
2.20.1



2022-06-27 15:55:09

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH v4] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 27.06.22 16:27, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>

Reviewed-by: Juergen Gross <[email protected]>


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2022-06-27 16:05:42

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v4] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 6/27/22 10:27, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 12 +++++++++++-
> 2 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99ad..a2eb375e2 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> void __init paravirt_set_cap(void)
> {
> if (!pv_is_native_spin_unlock())
> @@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
>
> if (!pv_is_native_vcpu_is_preempted())
> setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
> + else
> + static_branch_disable(&vcpu_has_preemption);
> }
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad52..adb41080d 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
> return node->cpu - 1;
> }
>
> +DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> +static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
> +{
> + if (!static_branch_unlikely(&vcpu_has_preemption))
> + return false;
> +
> + return vcpu_is_preempted(node_cpu(node->prev));
> +}
> +
> static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
> {
> int cpu_nr = encoded_cpu_val - 1;
> @@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
> * polling, be careful.
> */
> if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
> - vcpu_is_preempted(node_cpu(node->prev))))
> + vcpu_is_preempted_node(node)))
> return true;
>
> /* unqueue */

The patch looks good. I do have a minor nit though.

Usually, DEFINE_STATIC_KEY_TRUE() is paired with static_branch_likely()
and DEFINE_STATIC_KEY_FALSE() is paired with static_branch_unlikely(). I
think what Peter meant is to use DEFINE_STATIC_KEY_FALSE() and enable
vcpu_has_preemption together with X86_FEATURE_VCPUPREEMPT in the same if
block.

Cheers,
Longman

2022-06-28 04:59:56

by Guo Hui

[permalink] [raw]
Subject: [PATCH v5] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 6 +++++-
kernel/locking/osq_lock.c | 12 +++++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99..00f6059 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,11 +33,15 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_FALSE(vcpu_has_preemption);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
setup_force_cpu_cap(X86_FEATURE_PVUNLOCK);

- if (!pv_is_native_vcpu_is_preempted())
+ if (!pv_is_native_vcpu_is_preempted()) {
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ static_branch_enable(&vcpu_has_preemption);
+ }
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad..2909fad 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
return node->cpu - 1;
}

+DEFINE_STATIC_KEY_FALSE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+ if (!static_branch_unlikely(&vcpu_has_preemption))
+ return false;
+
+ return vcpu_is_preempted(node_cpu(node->prev));
+}
+
static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
{
int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* polling, be careful.
*/
if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+ vcpu_is_preempted_node(node)))
return true;

/* unqueue */
--
2.20.1




2022-06-28 13:11:19

by Guo Hui

[permalink] [raw]
Subject: [PATCH v6] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
kernel/locking/osq_lock.c | 12 +++++++++++-
2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99..a2eb375 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)

if (!pv_is_native_vcpu_is_preempted())
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ else
+ static_branch_disable(&vcpu_has_preemption);
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad..883e815 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
return node->cpu - 1;
}

+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+ if (static_branch_likely(&vcpu_has_preemption))
+ return vcpu_is_preempted(node_cpu(node->prev));
+
+ return false;
+}
+
static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
{
int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* polling, be careful.
*/
if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+ vcpu_is_preempted_node(node)))
return true;

/* unqueue */
--
2.20.1



2022-06-28 14:32:35

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v6] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 6/28/22 08:54, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 12 +++++++++++-
> 2 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99..a2eb375 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> void __init paravirt_set_cap(void)
> {
> if (!pv_is_native_spin_unlock())
> @@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
>
> if (!pv_is_native_vcpu_is_preempted())
> setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
> + else
> + static_branch_disable(&vcpu_has_preemption);
> }
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad..883e815 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
> return node->cpu - 1;
> }
>
> +DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> +static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
> +{
> + if (static_branch_likely(&vcpu_has_preemption))
> + return vcpu_is_preempted(node_cpu(node->prev));
> +
> + return false;
> +}
> +
> static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
> {
> int cpu_nr = encoded_cpu_val - 1;
> @@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
> * polling, be careful.
> */
> if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
> - vcpu_is_preempted(node_cpu(node->prev))))
> + vcpu_is_preempted_node(node)))
> return true;
>
> /* unqueue */

How about a further improvement on configurations that don't use
vcpu_is_preempted() at all?

+#ifdef vcpu_is_preempted
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
 static inline int node_cpu(struct optimistic_spin_node *node)
 {
        return node->cpu - 1;
 }

+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node
*node)
+{
+       if (static_branch_likely(&vcpu_has_preemption))
+               return vcpu_is_preempted(node_cpu(node->prev));
+
+       return false;
+}
+#else
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node
*node)
+{
+       return false;
+}
+#endif
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
 {
        int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
         * polling, be careful.
         */
        if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+                                 vcpu_is_preempted_node(node)))
                return true;

        /* unqueue */

For those configurations, vcpu_is_preempted_node() will just get
compiled out.

Cheers,
Longman

2022-06-28 16:26:41

by Guo Hui

[permalink] [raw]
Subject: [PATCH v7] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter
in the X86 architecture physical machine are redundant instructions,
causing the multi-core performance of Unixbench to drop by about 4% to 5%.
The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment,
causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction
in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
Double-Precision Whetstone 55.0 211986.3 38543.0
Execl Throughput 43.0 43453.2 10105.4
File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3
========
System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
Double-Precision Whetstone 55.0 211971.2 38540.2
Execl Throughput 43.0 45054.8 10477.9
File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8
========
System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8
File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0
========
System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0
File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2
========
System Benchmarks Index Score 8157.8

Signed-off-by: Guo Hui <[email protected]>
---
arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
kernel/locking/osq_lock.c | 19 ++++++++++++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
index 9e1ea99ad..a2eb375e2 100644
--- a/arch/x86/kernel/paravirt-spinlocks.c
+++ b/arch/x86/kernel/paravirt-spinlocks.c
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
__raw_callee_save___native_vcpu_is_preempted;
}

+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
void __init paravirt_set_cap(void)
{
if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)

if (!pv_is_native_vcpu_is_preempted())
setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+ else
+ static_branch_disable(&vcpu_has_preemption);
}
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index d5610ad52..f521b0f6d 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -27,6 +27,23 @@ static inline int node_cpu(struct optimistic_spin_node *node)
return node->cpu - 1;
}

+#ifdef vcpu_is_preempted
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+ if (static_branch_likely(&vcpu_has_preemption))
+ return vcpu_is_preempted(node_cpu(node->prev));
+
+ return false;
+}
+#else
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+ return false;
+}
+#endif
+
static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
{
int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
* polling, be careful.
*/
if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+ vcpu_is_preempted_node(node)))
return true;

/* unqueue */
--
2.20.1



2022-06-28 17:35:09

by Waiman Long

[permalink] [raw]
Subject: Re: [PATCH v7] x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

On 6/28/22 12:12, Guo Hui wrote:
> The instructions assigned to the vcpu_is_preempted function parameter
> in the X86 architecture physical machine are redundant instructions,
> causing the multi-core performance of Unixbench to drop by about 4% to 5%.
> The C function is as follows:
> static bool vcpu_is_preempted(long vcpu);
>
> The parameter 'vcpu' in the function osq_lock
> that calls the function vcpu_is_preempted is assigned as follows:
>
> The C code is in the function node_cpu:
> cpu = node->cpu - 1;
>
> The instructions corresponding to the C code are:
> mov 0x14(%rax),%edi
> sub $0x1,%edi
>
> The above instructions are unnecessary
> in the X86 Native operating environment,
> causing high cache-misses and degrading performance.
>
> This patch uses static_key to not execute this instruction
> in the Native runtime environment.
>
> The patch effect is as follows two machines,
> Unixbench runs with full core score:
>
> 1. Machine configuration:
> Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
> CPU core: 40
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 948326591.2 81261.9
> Double-Precision Whetstone 55.0 211986.3 38543.0
> Execl Throughput 43.0 43453.2 10105.4
> File Copy 1024 bufsize 2000 maxblocks 3960.0 438936.2 1108.4
> File Copy 256 bufsize 500 maxblocks 1655.0 118197.4 714.2
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1534674.7 2646.0
> Pipe Throughput 12440.0 46482107.6 37365.0
> Pipe-based Context Switching 4000.0 1915094.2 4787.7
> Process Creation 126.0 85442.2 6781.1
> Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
> Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
> System Call Overhead 15000.0 4714906.1 3143.3
> ========
> System Benchmarks Index Score 7923.3
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 947032915.5 81151.1
> Double-Precision Whetstone 55.0 211971.2 38540.2
> Execl Throughput 43.0 45054.8 10477.9
> File Copy 1024 bufsize 2000 maxblocks 3960.0 515024.9 1300.6
> File Copy 256 bufsize 500 maxblocks 1655.0 146354.6 884.3
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1679995.9 2896.5
> Pipe Throughput 12440.0 46466394.2 37352.4
> Pipe-based Context Switching 4000.0 1898221.4 4745.6
> Process Creation 126.0 85653.1 6797.9
> Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
> Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
> System Call Overhead 15000.0 4658746.7 3105.8
> ========
> System Benchmarks Index Score 8248.8
>
> 2. Machine configuration:
> Hygon C86 7185 32-core Processor
> CPU core: 128
> Memory: 256G
> OS Kernel: 5.19-rc3
>
> Before using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
> Double-Precision Whetstone 55.0 438969.9 79812.7
> Execl Throughput 43.0 10108.6 2350.8
> File Copy 1024 bufsize 2000 maxblocks 3960.0 275892.8 696.7
> File Copy 256 bufsize 500 maxblocks 1655.0 72082.7 435.5
> File Copy 4096 bufsize 8000 maxblocks 5800.0 925043.4 1594.9
> Pipe Throughput 12440.0 118905512.5 95583.2
> Pipe-based Context Switching 4000.0 7820945.7 19552.4
> Process Creation 126.0 31233.3 2478.8
> Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
> Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
> System Call Overhead 15000.0 6816047.5 4544.0
> ========
> System Benchmarks Index Score 7756.6
>
> After using the patch:
> System Benchmarks Index Values BASELINE RESULT INDEX
> Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
> Double-Precision Whetstone 55.0 451847.2 82154.0
> Execl Throughput 43.0 10595.1 2464.0
> File Copy 1024 bufsize 2000 maxblocks 3960.0 301279.3 760.8
> File Copy 256 bufsize 500 maxblocks 1655.0 79291.3 479.1
> File Copy 4096 bufsize 8000 maxblocks 5800.0 1039755.2 1792.7
> Pipe Throughput 12440.0 118701468.1 95419.2
> Pipe-based Context Switching 4000.0 8073453.3 20183.6
> Process Creation 126.0 33440.9 2654.0
> Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
> Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
> System Call Overhead 15000.0 6834371.5 4556.2
> ========
> System Benchmarks Index Score 8157.8
>
> Signed-off-by: Guo Hui <[email protected]>
> ---
> arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
> kernel/locking/osq_lock.c | 19 ++++++++++++++++++-
> 2 files changed, 22 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/paravirt-spinlocks.c b/arch/x86/kernel/paravirt-spinlocks.c
> index 9e1ea99ad..a2eb375e2 100644
> --- a/arch/x86/kernel/paravirt-spinlocks.c
> +++ b/arch/x86/kernel/paravirt-spinlocks.c
> @@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
> __raw_callee_save___native_vcpu_is_preempted;
> }
>
> +DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> void __init paravirt_set_cap(void)
> {
> if (!pv_is_native_spin_unlock())
> @@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
>
> if (!pv_is_native_vcpu_is_preempted())
> setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
> + else
> + static_branch_disable(&vcpu_has_preemption);
> }
> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
> index d5610ad52..f521b0f6d 100644
> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -27,6 +27,23 @@ static inline int node_cpu(struct optimistic_spin_node *node)
> return node->cpu - 1;
> }
>
> +#ifdef vcpu_is_preempted
> +DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
> +
> +static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
> +{
> + if (static_branch_likely(&vcpu_has_preemption))
> + return vcpu_is_preempted(node_cpu(node->prev));
> +
> + return false;
> +}
> +#else
> +static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
> +{
> + return false;
> +}
> +#endif
> +
> static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
> {
> int cpu_nr = encoded_cpu_val - 1;
> @@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
> * polling, be careful.
> */
> if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
> - vcpu_is_preempted(node_cpu(node->prev))))
> + vcpu_is_preempted_node(node)))
> return true;
>
> /* unqueue */
Reviewed-by: Waiman Long <[email protected]>